package com.tuju.jetpackfirstdemo.network

import com.coolweather.coolweatherjetpack.data.model.weather.HeWeather
import com.tuju.jetpackfirstdemo.data.model.City
import com.tuju.jetpackfirstdemo.data.model.County
import com.tuju.jetpackfirstdemo.data.model.Province
import com.tuju.jetpackfirstdemo.network.api.PlaceService
import com.tuju.jetpackfirstdemo.network.api.WeatherService
import retrofit2.Callback

class CoolWeatherNetwork {

    private val placeService = ServiceCreate.create(PlaceService::class.java)
    private val weatherService = ServiceCreate.create(WeatherService::class.java)

    //获取省份列表
    fun fetchProvinceList(callBack: Callback<List<Province>>) = placeService.getProvinces().enqueue(callBack)

    //获取城市列表
    fun fetchCityList(provinceId: Int, callBack: Callback<List<City>>) = placeService.getCities(provinceId).enqueue(callBack)

    //获取区列表
    fun fetchCountyList(provinceId: Int, cityId: Int, callback: Callback<List<County>>) = placeService.getCounties(provinceId, cityId).enqueue(callback)

    //获取天气
    fun fetchWeather(weatherId: String, key: String, callback: Callback<HeWeather>) = weatherService.getWeather(weatherId, key).enqueue(callback)

    //获取Bing大图
    fun fetchBingPic(callBack: Callback<String>) = weatherService.getBingPck().enqueue(callBack)

    companion object {
        private var network: CoolWeatherNetwork? = null

        fun getInstance(): CoolWeatherNetwork? {
            if (network == null) {
                synchronized(CoolWeatherNetwork::class.java) {
                    if (network == null) {
                        network = CoolWeatherNetwork()
                    }
                }
            }
            return network
        }
    }
}